// 名字
var aq=localStorage.getItem("账号")
  var yong=document.querySelector("#a-1")
  console.log(yong.innerHTML)
  yong.innerHTML=aq

// 显示数据
var url=`http://jx.xuzhixiang.top/ap/api/cart-list.php`
  $.get(url,{
    id:localStorage.getItem("id"),  
   
  },(res)=>{
    console.log(res.data)
   
    var data=res.data
    var html=""
    data.forEach(item=> {
      html+=`
       <li style="position: relative;">
       <input class="dan" onchange="dan()" style="width: 20px;
       height: 20px;border: 1px solid #cacaca;" type="checkbox">
       <img class="img-z" class="a110" src="${item.pimg}">　　　　　　　　　
       <span class="spa-z1">${item.pname}</span>　　　　　　　　　　　　　　　　　　　     
       <span class="price" class="spa-z1">${item.pprice}</span>
      <span class="spa-z2">
       <button class="jia"onclick="jia(${item.pid},this)">+</button>
       <span class="spa-z3">1</span>
       <button class="jian" onclick="jian(${item.pid},this)">-</button>
       <span class="zong1"></span></span>
       <button onclick="chu(${item.pid},this)" class="bttn">X</button>
       </li>
       
       <hr>
      `
    
 
  })
 var oul=  document.querySelector("ul")
    oul.innerHTML=html



});
// 删除
function chu(pid,btn){ 
   foo()
  console.log(btn)
  $.get(`http://jx.xuzhixiang.top/ap/api/cart-delete.php`,{
    uid:localStorage.getItem("id"),
    pid:pid
  },(res)=>{
    console.log(res)
    btn.parentNode.remove();
        countFn();  
       
  })

}
// 加
  function jia(pid,btn){
    console.log(btn)
   var s1= btn.nextElementSibling
   s1.innerHTML=parseInt(s1.innerHTML)+1
   var pnum= s1.innerHTML
  $.get(`http://jx.xuzhixiang.top/ap/api/cart-update-num.php`,{
    uid:localStorage.getItem("id"),
    pid:pid,
    pnum:pnum
  },(res)=>{
    console.log(res)
  })
  foo()
  }
// 减
function jian(pid,btn){
  
 s1= btn.previousElementSibling
  s1.innerHTML=parseInt(s1.innerHTML)-1
  if(s1.innerHTML<=1){
    s1.innerHTML=1
  }
  var pnum= s1.innerHTML
    $.get(`http://jx.xuzhixiang.top/ap/api/cart-update-num.php`,{
      uid:localStorage.getItem("id"),
      pid:pid,
      pnum:pnum
    },(res)=>{
      console.log(res)
    })
    foo()
}
  // 全选all
function quan(){ 
  var dan=document.querySelectorAll(".dan") 
 var quan=document.querySelector(".quan")
 dan.forEach((v)=>v.checked=quan.checked)
  foo()
}

// 单选sing
function dan(){ 
  var dan=document.querySelectorAll(".dan") 
 var quan=document.querySelector(".quan")
  var arr=[...dan]
 var qu=arr.every((v)=>v.checked==true)
  quan.checked=qu
  console.log(dan)
  foo()
}

// 计算总价
function foo(){
  var dan=document.querySelectorAll(".dan");
  let arr=[...dan]  //转真数组
  var res=arr.filter((v)=>v.checked==true)
    // 总价
   var num=0
    res.forEach(item=>{
       var li=item.parentNode
       
       var p=li.querySelector(".price").innerHTML
       
       var pnum=li.querySelector(".spa-z3").innerHTML 
        var count=p*pnum 
       num=num+count
       console.log(num) 
    })
  var zongjia=document.querySelector(".spa-z4")
  console.log(zongjia )
  zongjia.innerHTML=num

}

function ff(){
  var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
  setTimeout(function(){
    location.href="index.html"
  },1000)
}

// 退出
function tuichu(){
  var s="🎎登录　|"
  var a=localStorage.removeItem("账号")
  var a1=localStorage.getItem("账号")
  var a2=document.querySelector("#a-1")
  console.log(a)
  if(a1==null){
    layer.confirm('亲确定要退出吗？', {
      btn: ['确定','取消'] //按钮
    }, function(){
       location.href="登录1.html"
    }, function(){
    });
 
  }else{
    a2.innerHTML=s
  }
}

// 购物点击
$("#gouwu").click(function(){
  var a1=localStorage.getItem("账号")
  if(a1==null){
    layer.confirm('亲，您还没登录', {
      btn: ['登录','继续看看'] //按钮
    }, function(){
       location.href="登录1.html"
    }, function(){
    });
  }else{
    layer.msg('亲，您已在本页面中');
  }
})